﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

public partial class _Default : System.Web.UI.Page
{
    /// <summary>
    /// Page Load - not used
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
    }


    /// <summary>
    /// Create list of properties from a JSON string
    /// </summary>
    /// <param name="jsonString"></param>
    /// <returns></returns>
    private List<CityProperty> GetProperties(string jsonString)
    {
        List<CityProperty> properties = new List<CityProperty>();

        JavaScriptSerializer ser = new JavaScriptSerializer();

        object o = ser.DeserializeObject(jsonString);

        object[] oList = o as object[];


        for (int i = 0; i < oList.Length; i++)
        {
            CityProperty cityProp = new CityProperty();

            Dictionary<string, object> foo = oList[i] as Dictionary<string, object>;

            cityProp.Id = foo["ID"] as string;
            cityProp.Owner1 = foo["OWNER1"] as string;
            cityProp.Owner2 = foo["OWNER2"] as string;
            cityProp.Owner3 = foo["OWNER3"] as string;

            object value = foo["VAL"];

            if( value != null)
            {
                cityProp.TotalValue = (int)value;
            }

            properties.Add(cityProp);
        }

        return properties;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.GridView1.DataSource = GetProperties(textbox1.Value);
        this.GridView1.DataBind();
    }
}
